package com.example.equipment.controller;

import com.example.equipment.bean.Product;
import com.example.equipment.bean.enums.ResultEnum;
import com.example.equipment.bean.User;
import com.example.equipment.bean.config.OpenApiResult;
import com.example.equipment.service.ProductService;
import com.github.pagehelper.ISelect;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * 产品处理
 */
@RestController
@Slf4j
@RequestMapping("/api/product")
public class ProductController {

    @Autowired
    private ProductService productService;
    private Gson gson = new GsonBuilder().create();

    /**
     * 新增产品
     *
     * @param product
     * @param request
     * @return
     */
    @PostMapping("/add")
    public OpenApiResult addProduct(@RequestBody Product product, HttpServletRequest request) {
        log.info("==> ProductController.addProduct(). product={}", gson.toJson(product));
        if (StringUtils.isEmpty(product.getProductName())
                || "".equals(product.getProductName().trim())) {
            return new OpenApiResult(ResultEnum.REQUESTPARAMERROR);
        }
        if (StringUtils.isEmpty(product.getProductNo())
                || "".equals(product.getProductNo().trim())) {
            return new OpenApiResult(ResultEnum.REQUESTPARAMERROR);
        }

        User user = (User) request.getSession().getAttribute("user");
        product.setCreateBy(user.getName());
        product.setUpdateBy(user.getName());
        product.setCreateTime(new Date());
        product.setUpdateTime(product.getUpdateTime());
        return productService.addProduct(product);

    }

    /**
     * 删除产品信息
     *
     * @param
     * @return
     */
    @PostMapping("/delete")
    public OpenApiResult deleteById(@RequestBody Product product, HttpServletRequest request) {
        log.info("==> ProductController.deleteById(). id={}", product.getId());
        User user = (User) request.getSession().getAttribute("user");
        product.setUpdateBy(user.getName());
        productService.deleteById(product);
        return OpenApiResult.ok();
    }

    /**
     * 修改产品信息
     *
     * @param
     * @return
     */
    @PostMapping("/update")
    public OpenApiResult updateById(@RequestBody Product product, HttpServletRequest request) {
        log.info("==> ProductController.updateById(). product={}", gson.toJson(product));
        if (product == null || product.getId() == null) {
            return new OpenApiResult(ResultEnum.REQUESTPARAMERROR);
        }
        User user = (User) request.getSession().getAttribute("user");
        product.setUpdateBy(user.getName());
        product.setUpdateTime(new Date());
        try {
            productService.updateById(product);
        } catch (Exception e) {
            log.error("修改异常，e=", e);
            return OpenApiResult.fail(e.getMessage());
        }

        return OpenApiResult.ok();
    }

    /**
     * 获取测试记录列表
     *
     * @param
     * @return
     */
    @GetMapping("/list")
    public OpenApiResult<PageInfo<Product>> getList(@RequestParam(value = "page", defaultValue = "1") Integer page,
                                                    @RequestParam(value = "pageSize", defaultValue = "15") Integer pageSize,
                                                    @RequestParam(value = "productNo", required = false) String productNo,
                                                    @RequestParam(value = "productName", required = false) String productName) {
        log.info("==> ProductController.getList(). productNo={},productName={}", productNo, productName);
        PageInfo<Product> info = PageHelper.startPage(page, pageSize).doSelectPageInfo((ISelect) () -> {
            productService.getList(productNo, productName);
        });

        return OpenApiResult.ok(info);
    }
}
